
package example

import (
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/example"
    exampleReq "github.com/flipped-aurora/gin-vue-admin/server/model/example/request"
)

type UniversityStudentScoreService struct {}
// CreateUniversityStudentScore 创建学成绩记录
// Author [yourname](https://github.com/yourname)
func (USSService *UniversityStudentScoreService) CreateUniversityStudentScore(USS *example.UniversityStudentScore) (err error) {
	err = global.GVA_DB.Create(USS).Error
	return err
}

// DeleteUniversityStudentScore 删除学成绩记录
// Author [yourname](https://github.com/yourname)
func (USSService *UniversityStudentScoreService)DeleteUniversityStudentScore(ID string) (err error) {
	err = global.GVA_DB.Delete(&example.UniversityStudentScore{},"id = ?",ID).Error
	return err
}

// DeleteUniversityStudentScoreByIds 批量删除学成绩记录
// Author [yourname](https://github.com/yourname)
func (USSService *UniversityStudentScoreService)DeleteUniversityStudentScoreByIds(IDs []string) (err error) {
	err = global.GVA_DB.Delete(&[]example.UniversityStudentScore{},"id in ?",IDs).Error
	return err
}

// UpdateUniversityStudentScore 更新学成绩记录
// Author [yourname](https://github.com/yourname)
func (USSService *UniversityStudentScoreService)UpdateUniversityStudentScore(USS example.UniversityStudentScore) (err error) {
	err = global.GVA_DB.Model(&example.UniversityStudentScore{}).Where("id = ?",USS.ID).Updates(&USS).Error
	return err
}

// GetUniversityStudentScore 根据ID获取学成绩记录
// Author [yourname](https://github.com/yourname)
func (USSService *UniversityStudentScoreService)GetUniversityStudentScore(ID string) (USS example.UniversityStudentScore, err error) {
	err = global.GVA_DB.Where("id = ?", ID).First(&USS).Error
	return
}
// GetUniversityStudentScoreInfoList 分页获取学成绩记录
// Author [yourname](https://github.com/yourname)
func (USSService *UniversityStudentScoreService)GetUniversityStudentScoreInfoList(info exampleReq.UniversityStudentScoreSearch) (list []example.UniversityStudentScore, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
    // 创建db
	db := global.GVA_DB.Model(&example.UniversityStudentScore{})
    var USSs []example.UniversityStudentScore
    // 如果有条件搜索 下方会自动创建搜索语句
    if info.StartCreatedAt !=nil && info.EndCreatedAt !=nil {
     db = db.Where("created_at BETWEEN ? AND ?", info.StartCreatedAt, info.EndCreatedAt)
    }
	err = db.Count(&total).Error
	if err!=nil {
    	return
    }
        var OrderStr string
        orderMap := make(map[string]bool)
         	orderMap["student_id"] = true
         	orderMap["course_id"] = true
         	orderMap["score"] = true
         	orderMap["grade"] = true
         	orderMap["term"] = true
       if orderMap[info.Sort] {
          OrderStr = info.Sort
          if info.Order == "descending" {
             OrderStr = OrderStr + " desc"
          }
          db = db.Order(OrderStr)
       }

	if limit != 0 {
       db = db.Limit(limit).Offset(offset)
    }

	err = db.Find(&USSs).Error
	return  USSs, total, err
}
func (USSService *UniversityStudentScoreService)GetUniversityStudentScorePublic() {
    // 此方法为获取数据源定义的数据
    // 请自行实现
}
